package com.inet.receiver;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONException;
import org.json.JSONObject;






import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.inet.request.ServerRequest;
import com.inet.util.L;
import com.inet.util.Utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;

public class ReceiverServices extends BroadcastReceiver {

	private Context pContext;
	private MyHandler myHandler;
	private static ExecutorService cachedThreadPool = Executors
			.newCachedThreadPool();
	
	
	@Override
	public void onReceive(final Context context, Intent intent) {
		pContext = context;
		myHandler=new MyHandler(pContext);
		
		//开机的时候， 检测是否非法使用，如果是，打开WarningActivity
		if(Settings.System.getInt(context.getContentResolver(),
				"is_user_lock", 0) == 1){
			PushOption.closeLauncher(null,pContext,myHandler);
		}
		
		Bundle bundle = intent.getExtras();
		L.i("onReceive() action=" + bundle.getInt("action"));

		switch (bundle.getInt(PushConsts.CMD_ACTION)) {
		case PushConsts.GET_MSG_DATA:
			// 获取透传数据
			byte[] payload = bundle.getByteArray("payload");
			if (payload != null) {
				final String data = new String(payload);
				L.i("data==========" + data);
				cachedThreadPool.execute(new Runnable() {
					
					@Override
					public void run() {
						JSONObject obj;
						try {
							obj = new JSONObject(data);
							parseType(obj);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
			}
			break;

		case PushConsts.GET_CLIENTID:
			// 获取ClientID(CID)
			// 第三方应用需要将CID上传到第三方服务器，并且将当前用户帐号和CID进行关联，以便日后通过用户帐号查找CID进行消息推送
			final String cid = bundle.getString("clientid");
			L.i("onReceive() cid==" + cid);
			final String imei = Utils.getImei(context);
			new Thread(new Runnable() {
				@Override
				public void run() {
					//车机注册
					ServerRequest.registerClient(context, imei, cid);
					ServerRequest.sendICCID(context, imei);
				}
			}).start();
			break;
		case PushConsts.GET_SDKONLINESTATE:
			boolean online = bundle.getBoolean("onlineState");
			L.i("online = " + online);
			if(online){
				//1 是打开云
				Settings.System.putInt(context.getContentResolver(), "isOpenCloud", 1);
				
			}else{
				Settings.System.putInt(context.getContentResolver(), "isOpenCloud", 0);
				
			}
			Intent connectIntent = new Intent();
			connectIntent.setAction("connected_action");
			connectIntent.putExtra("connected_action_key",
					online);
			context.sendBroadcast(connectIntent);
			break;

		default:
			break;
		}

	}

	/**
	 * 个推透传类型解析
	 * 
	 * @param obj
	 */
	public void parseType(JSONObject obj) {
		String type = null;
		try {
			type = obj.getString("type");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		L.i("this push type is " + type);
		switch (type) {
		case "updateLastTime":  //更新客户使用时间
			break;
		case "getPostion":    //位置获取
			PushOption.getCarPoint(pContext); 
			break;
		case "closeSystem":   //关机
			PushOption.closeSystem(obj,pContext);
			break;
		case "remoteCommand":  //预约导航
			PushOption.startNav(obj, pContext);
			break;
		case "showWay":        //好友指路
			PushOption.startFriendNav(obj, pContext);
			break;
		case "openCrawl":      //打开电子围栏
			PushOption.openCrawl(obj, pContext);
			break;
		case "closeCrawl":     //关闭电子围栏
			PushOption.closeCrawl(obj, pContext);
			break;
		case "getPhoto":     //拍照  车机无此功能
			PushOption.getPhoto(obj,pContext);
			break;
		case "getVideo":     //录像  车机无此功能
			PushOption.getVideo(obj,pContext);
			break;
		case "closeLauncher":      //警告界面打开
			PushOption.closeLauncher(obj,pContext,myHandler);
			break;
		case "openLauncher":           //警告界面关闭
			PushOption.openLauncher(obj,pContext,myHandler);
			break;
		case "video_xyw":       //广告推送 附带参数为网址
			PushOption.startVideo(obj,myHandler);
			break;
		default:
			break;
		}
	}
}
